package com.goldsprite.microhorigame.screens.tests;

import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.goldsprite.gameframeworks.assets.FontUtils;
import com.goldsprite.gameframeworks.assets.GlobalAssets;
import com.goldsprite.gameframeworks.input.widgets.VirtualButton;
import com.goldsprite.gameframeworks.input.widgets.VirtualJoystick;
import com.goldsprite.gameframeworks.utils.math.Vector2;
import com.goldsprite.microhorigame.screens.basics.ExampleGScreen;

import java.util.function.Consumer;
import com.goldsprite.gameframeworks.input.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.graphics.*;

public class InputSystemExample extends ExampleGScreen {
	private Stage stage;
	private VirtualJoystick joystick;
	private VirtualButton attackBtn;
	private VirtualButton jumpBtn;

	// 添加虚拟按键测试相关变量
	private String lastKeyPressed = "None";
	private float keyDisplayTime = 0f;
	private BitmapFont debugFont;

	private SpriteBatch batch; // 添加字体变量

	@Override
	public String getIntroduction() {
		int k2;
		String msg = "按键情况: \n" + InputSystem.lastPrintLog;
		return "" +
			"多平台输入系统测试: " + "\n" +
			"	测试桌面键盘输入与安卓虚拟键盘输入" + "\n" +
			"	Vector2四方向输入: WASD与↑↓←→, Boolean按下抬起输入: J, K" + "\n" +
			msg + "\n" +
			""
			;
	}

	private GameInputSystem inputs;

	@Override
	public void create() {
		inputs = GameInputSystem.getInstance();

		registerInputActionsListeners();

		createStageUIs();


		// 初始化调试字体，使用项目工具类
		batch = new SpriteBatch();
		debugFont = FontUtils.generateAutoClarity(14);
		debugFont.setColor(Color.WHITE);
	}

	private void createStageUIs() {
		stage = new Stage(getViewport());
		getImp().addProcessor(stage);

		Skin skin = GlobalAssets.getInstance().editorSkin;
		// Touchpad 摇杆
		joystick = new VirtualJoystick(100, 0.3f, skin);
		joystick.setPosition(50, 50);
		stage.addActor(joystick);
		GameInputSystem.setVirtualJoystick(joystick);

		// 攻击按钮
		attackBtn = new VirtualButton("Attack", skin);
		attackBtn.setBounds(getViewSize().x - 250, 100, 200, 100);
		stage.addActor(attackBtn);
		GameInputSystem.setAttackVirButton(attackBtn);

		// 跳跃按钮
		jumpBtn = new VirtualButton("Jump", skin);
		jumpBtn.setBounds(getViewSize().x - 250, 250, 200, 100);
		stage.addActor(jumpBtn);
		GameInputSystem.setJumpVirButton(jumpBtn);
	}

	private void registerInputActionsListeners() {
		registerMoveListener();
		registerJumpListener();
	}

	private void registerMoveListener() {
		InputAction moveAction = inputs.getInputAction("Move");
		Consumer<Vector2> moveListener = keyDir -> {
		};
		inputs.registerActionListener(moveAction, InputActionState.Started, moveListener);
		inputs.registerActionListener(moveAction, InputActionState.Performed, moveListener);
		inputs.registerActionListener(moveAction, InputActionState.Canceled, moveListener);
	}

	private void registerJumpListener() {
		InputAction jumpAction = inputs.getInputAction("Jump");
		Consumer<Boolean> jumpListener = down -> {
		};
		inputs.registerActionListener(jumpAction, InputActionState.Started, jumpListener);
		inputs.registerActionListener(jumpAction, InputActionState.Performed, jumpListener);
		inputs.registerActionListener(jumpAction, InputActionState.Canceled, jumpListener);
	}

	@Override
	public void show() {
		super.show();
		GameInputSystem.showAndroidKeyBoard(true);
	}

	@Override
	public void hide() {
		super.hide();
		GameInputSystem.showAndroidKeyBoard(false);
	}

	@Override
	public void render(float delta) {
		super.render(delta);

		stage.act(delta);
		stage.draw();
	}

}
